package com.menglei.dailytasks.m0716;

import android.Manifest;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;

import com.menglei.dailytasks.R;

public class AnOtherActivity extends AppCompatActivity implements View.OnClickListener{
    private Button mBtnQueryApp;

    private Button mBtnInsert;

    private Button mBtnQueryContact;
    private Uri uri;


    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_another);
        initView();
        uri = Uri.parse("content://com.example.demo0716.custom.SimpleContentProvider/users");
        if (!hasPermission(this)) {
            //Log.d("是否有权限：", "是的");
            requestPermissions(new String[]{Manifest.permission.READ_CONTACTS}, 0);
        }

    }

    //判断是否有权限
    private boolean hasPermission(Context context) {
        int result = ContextCompat.checkSelfPermission(context, Manifest.permission.READ_CONTACTS);

        return result == 0;
    }

    private void initView() {
        mBtnQueryApp = (Button) findViewById(R.id.btn_query_app);
        mBtnQueryApp.setOnClickListener(this);
        mBtnInsert = (Button) findViewById(R.id.btn_insert);
        mBtnInsert.setOnClickListener(this);
        mBtnQueryContact = (Button) findViewById(R.id.btn_query_contact);
        mBtnQueryContact.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            default:
                break;
            case R.id.btn_query_app:
                Cursor query = getContentResolver().query(uri, null, null, null, null);
                while (query.moveToNext()) {
                    Log.d("MainActivity", query.getString(query.getColumnIndex("name")));
                }
                break;
            case R.id.btn_insert:
                ContentValues values = new ContentValues();
                values.put("name", "另一个App");
                getContentResolver().insert(uri, values);
                break;
            case R.id.btn_query_contact:
                ContentResolver resolver = getContentResolver();
                Cursor cursor = resolver.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
                Toast.makeText(this, "通讯录共有：" + cursor.getCount() + "人", Toast.LENGTH_SHORT).show();
                while (cursor.moveToNext()) {
                    // 取得联系人ID
                    String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
                    String contactName = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));

                    // 根据联系人ID查询对应的电话号码
                    Cursor phoneNumbers = resolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = "
                            + contactId, null, null);
                    while (phoneNumbers.moveToNext()) {
                        String number = phoneNumbers.getString(phoneNumbers.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                        Log.d("通讯录", "姓名：" + contactName + "\t" + "手机号：" + number);
                    }
                    phoneNumbers.close();
                }
                cursor.close();
                break;
        }
    }
}
